package windowUnits;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Insets;
import java.util.Calendar;

import javaUtils.DateUtils;

import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import javax.swing.table.TableCellRenderer;

public class ButtonRenderer extends JButton implements TableCellRenderer {
	
	private Calendar currDate;
	
	public ButtonRenderer() {
		init();
	}
	
	public ButtonRenderer(Calendar cal) {
		this.currDate = cal;
		init();
	}
	
	public ButtonRenderer(Icon arg0) {
		super(arg0);
		init();
	}

	public ButtonRenderer(String arg0) {
		super(arg0);
		init();
	}

	public ButtonRenderer(Action arg0) {
		super(arg0);
		init();
	}

	public ButtonRenderer(String arg0, Icon arg1) {
		super(arg0, arg1);
		init();
	}
	
	private void init(){
		setOpaque(true);
		setBorder(new LineBorder(new Color(213,213,213), 1, false));
		setBackground(new Color(232,232,232));
		setFont(new Font("", Font.PLAIN, 10));
		setMargin(new Insets(0, 0, 0, 0));
		setBounds(0, 0, 18, 18);
	}
	
	//@Override
	public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
		if (isSelected) {
	      setForeground(table.getSelectionForeground());
	      setBackground(table.getSelectionBackground());
	    } else{
	      setForeground(table.getForeground());
	      if(currDate != null)
	      if(DateUtils.getDateNow(DateUtils.DF_MM_YYYY, Calendar.getInstance()).equals(DateUtils.getDateNow(DateUtils.DF_MM_YYYY, currDate)))
	    	  if(DateUtils.getDateNow(DateUtils.DF_DD, Calendar.getInstance()).equals(value.toString()))
	    		  setBackground(new Color(72,72,72));
	    	  else
	    		  setBackground(new Color(232,232,232)); 
	      else
	    	  setBackground(new Color(232,232,232));
	    }
	    setText( (value ==null) ? "" : value.toString() );
	    if(value.toString().length() == 0){
	    	setEnabled(false);
	    	setFocusable(false);
	    }else{
	    	setEnabled(true);
	    	setFocusable(true);
	    }
	    
	    return this;
	}

}
